<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="登录">
      <!-- <template #left>
                <van-icon name="cross" size="18"/>
      </template>-->
      <template slot="left">
        <van-icon name="cross" size="18"/>
      </template>
    </van-nav-bar>
    <van-form @submit="onSubmit">
      <van-field v-model="user.mobile" name="用户名" placeholder="用户名" :rules="loginRule.mobile">
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="密码"
        placeholder="密码"
        left-icon="toutiao toutiao-yanzhengma"
        :rules="loginRule.code"
      >
        <!-- 使用插槽实现图标布局 -->
        <template slot="left-icon">
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>
        <template #button>
          <van-button
            v-if="timeFlag"
            @click.prevent="semdCode"
            round
            color="#ccc"
            size="small"
            type="primary"
          >发送验证码</van-button>
          <van-count-down v-else :time="time" format="ss s" @finish="finish"/>
        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>
           
<script>
import { login_api, codes_api } from '@/api/login.js'

export default {
  data() {
    return {
      user: {
        mobile: '13911111112',
        code: '246810'
      },
      loginRule: {
        mobile: [
          { required: true, message: '请填写用户名' },
          { pattern: /^1[3|5|8|9]\d{9}$/, message: '请填写合法的手机号' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '请填写合法的验证码' }
        ]
      },
      time: 5000,
      timeFlag: true
    }
  },
  methods: {
    //点击提交按钮
    async onSubmit() {
      // 开始转圈圈
      // this.$toast.loading({
      //   duration: 0, // 持续时间，0表示持续展示不停止
      //   forbidClick: true, // 是否禁止背景点击
      //   message: '登录中...' // 提示消息
      // })

      try {
        const { data: res } = await login_api(this.user)
        console.log(res, 4444)
        //this.$toast('登录成功')

        this.$store.commit('SET_USER', res.data)
        // 实现页面跳转   my
        this.$router.push('/my')
      } catch (err) {
        // if (err.response.status == 403) {
        //   this.$toast('请先获取验证码')
        // }
      }

      // setTimeout(async () => {
      //   try {
      //     const { data: res } = await login_api(this.user)
      //     console.log(res, 4444)
      //     this.$toast('登录成功')
      //     window.localStorage.setItem('token', JSON.stringify(res.data))
      //   } catch (err) {
      //     if (err.response.status == 403) {
      //       this.$toast('请先获取验证码')
      //     }
      //   }
      // }, 3000)

      // const {data:res}=await login_api(this.user).catch(err=>{
      //   console.log(err);
      // })
      // console.log(res,4444);
    },
    //  点击按钮获取验证码
    async semdCode() {
      this.timeFlag = !this.timeFlag
      try {
        const { data: res } = await codes_api(this.user.mobile)
        console.log(res, 888)
      } catch (err) {
        this.$toast('获取验证码失败')
      }
    },
    //  监听倒计时走完毕
    finish() {
      console.log(11111)
      this.timeFlag = !this.timeFlag
    }
  }
}
</script>
<style scoped  lang='less'>
.login-container {
  .van-nav-bar {
    background-color: #3296fa;
    .van-icon {
      color: #fff;
    }
    // /deep/.van-nav-bar__title {
    //   color: #fff;
    // }
  }

  .toutiao {
    font-size: 40px;
  }

  .van-button--info {
    color: #fff;
    background-color: #1989fa;
    opacity: 0.5;
  }
}
</style>